#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Project: spd-sxmcc
'''
@author: lyndon
@time Created on 2018/9/11 11:15
@desc
'''

import os
from flask import Flask, Response
from flask import render_template, jsonify

# Flask初始化参数尽量使用你的包名，这个初始化方式是官方推荐的，官方解释：http://flask.pocoo.org/docs/0.12/api/#flask.Flask
# app = Flask(__name__)
from rest import app

imgPathCfg = "D:/iProject/myPython/com/teradata/analyzer/data/{}"


@app.route('/HelloWorld')
def hello_world():
    return "Hello World!"


@app.route("/image/<imageid>")
def img(imageid):
    return get_image(imageid)
    # imgPath = imgPathCfg.format(imageid)
    # mdict = {
    #     'jpeg': 'image/jpeg',
    #     'jpg': 'image/jpeg',
    #     'png': 'image/png',
    #     'gif': 'image/gif'
    # }
    # mime = mdict[(imageid.split('.')[1])]
    # with open(imgPath, 'rb') as f:
    #     image = f.read()
    # return Response(image, mimetype=mime)


def get_image(uri):
    imgPath = imgPathCfg.format(uri)
    mdict = {
        'jpeg': 'image/jpeg',
        'jpg': 'image/jpeg',
        'png': 'image/png',
        'gif': 'image/gif'
    }
    mime = mdict[(uri.split('.')[1])]
    # if not os.path.exists(imgPath):
    #     # Res 是我自己定义的返回类，raw方法将数据组成字典返回
    #     return jsonify(Res(0, 'image does not exists').raw())
    with open(imgPath, 'rb') as f:
        image = f.read()
    return Response(image, mimetype=mime)


if __name__ == "__main__":
    # 这种是不太推荐的启动方式，我这只是做演示用，官方启动方式参见：http://flask.pocoo.org/docs/0.12/quickstart/#a-minimal-application
    app.run(debug=True)

